{include file="public/head"}
<body>
    <div class="appointment">
        <div class="appointment-h" style="display: block;"></div>
        <div class="com-flex ">
            <div class="t">预约时间：</div>
            <div class="time">
            	<select class="timeSelect"></select>
                <!--<img src="__STATIC__/home/img/image17.png" alt="..." />-->
            </div>
        </div>
        <div class="com-flex auto">
            <div class="t">预约时长：</div>
            <div class="allhour">{$type.shichang}分钟</div>
        </div>
        <!--<div class="com-flex auto">
            <div class="t">预约时段：</div>
            <div class="allhour">2019-05-14   08:20—10:00</div>
        </div>-->
        <div class="com-flex coachPeople">
            <div class="t">选择教练：</div>
            <div class="coach-wrap">
                <!--<div class="input-coach">
                    <div></div>
                    <img src="__STATIC__/home/img/image16.png" class="x" alt="..." />
                </div>
                <div class="coach">
                    <div class="coach-item">张某某</div>
                    <div class="coach-item">张某某</div>
                    <div class="coach-item">张某某</div>
                    <div class="coach-item">张某某</div>
                    <div class="coach-item">张某某</div>
                </div>-->
                <select></select>
            </div>
        </div>
        {:token()}
    </div>
    <div style="height: 1.2rem;"></div>
    <div class="footer">
        <div class="flex-center zaixian">
            	确认预约
        </div>
    </div>
    <script src="__STATIC__/home/js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
    <script src="__STATIC__/home/js/public.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
    	$(function(){
            // token令牌
            function token(cb) {
                $.ajax({
                    type: 'post',
                    url: '/index/Traincar/token',
                    success(res) {
                        if (res.code == 1) {
                            cb(res.__token__);
                        }
                    },
                    error() { alert('token令牌接口错误'); }
                })
            };
//  		获取页面参数
			var hrefUrl = window.location.href;
			var num=hrefUrl.indexOf("jzid")+5;
			var num2=hrefUrl.indexOf("&kmid=");
			var num3=hrefUrl.indexOf("&kmid=")+6;
			var numleng = hrefUrl.length;
			var hrefId = hrefUrl.substring(num,num2);
			var hrefId2 = hrefUrl.substring(num3,numleng);
			console.log("打印id",hrefId,hrefId2);
			var timeIDcent="";//获取教练列表的id
			var oneId="";//教练的id
			var timeSelectId="";//这个是时间段id
//			获取开始时间
			$.ajax({
                type: 'post',
                url: '/index/traincar/traincarSubmitData',
                data: {
                    "jzid":hrefId,
                    "kmid":hrefId2
                },
                success(res) {
                    if (res.code == 1) {
						timeSelectId=res.data[0][0].id;
						timeIDcent=res.data[0][0].dateid;
						caothData();
						datPeople();
	                    for(var k=0;k<res.data.length;k++){
	                    	for(var g=0;g<res.data[k].length;g++){
	                    		var timeId=res.data[k][g].dateid;//日期id,请求教练信息用的参数
	                    		var id=res.data[k][g].id;//时间段id确认订单时的参数
	                    		var timeWeek=res.data[k][g].date+' ~ '+res.data[k][g].time+' ~ '+res.data[k][g].week;
	                    		var typeCent='<option value="'+ timeId +'" data-id="'+ id +'">'+ timeWeek +'</option>';
	                    		$(".timeSelect").append(typeCent);
	                    	}
	                    }
                    }else if (res.code == 0){
                    	alert(res.msg);
                    }
                },
                error() {
                    alert('开始时间请求失败!');
                }
            });
//          当天预约名额人数
			function datPeople(){
				$.ajax({
	                type: 'post',
	                url: '/index/traincar/gettongzhi',
	                data: {
	                    "id":timeIDcent
	                },
	                success(res) {
	                    console.log("库存通知",res);
	                    if(res.code==1){
	                    	$(".appointment-h").show();
	                    	$(".appointment-h").text(res.data.date+'~ 总名额:'+res.data.totalcount+'人,还差'+res.data.shengcount+'人');
	                    }else{
	                    	// $(".appointment-h").hide();
	                    }
	                },
	                error() {
	                    alert('预约名额数据请求失败!');
	                }
	            });				
			}
//          开始时间选择
			$(document).on('change', '.timeSelect', function (e){
            	$(".coach-wrap>select").children().remove();
//			          自定义属性id值
			    timeSelectId=$(this).find("option:selected").data('id');
//			          日期id
			    timeIDcent=$(this).val();
			    console.log("00",timeIDcent);
			    caothData();
			    
			});
//			教练选择
			$(document).on('change', '.coach-wrap>select', function (e){
			    oneId=$(this).val();
			});
			//教练数据加载
			function caothData(){
				$.ajax({
	                type: 'post',
	                url: '/index/traincar/getcoach',
	                data: {
	                    "id":timeIDcent
	                },
	                success(res) {

	                    console.log("shuju",res);
	                    if (res.code == 1) {
	                    	console.log("111",res.data.length)
	                    	for(var k=0;k<res.data.length;k++){
	                    		console.log("222")
	                    		var id=res.data[k].id;
	                    		oneId=res.data[0].id;
	                    		var name=res.data[k].name;
	                    		var centTxt='<option value="'+ id +'">'+ name +'</option>';
	                    		$(".coach-wrap>select").append(centTxt);
	                    	}
	                    }else{
	                    	alert(res.msg);
	                    }
	                },
	                error() {
	                    alert('教练数据请求失败!');
	                }
	            });
			}
//			确认预约
			$(".footer").click(function(){
			    var token=$('input[name="__token__"]').val();
				if(timeSelectId==""||timeSelectId==null){
					alert("当天没有教练!");return false;
				}
				$.ajax({
	                type: 'post',
	                url: '/index/traincar/ordersubmit',
	                data: {
	                    "coachid":oneId,
	                    "timeid":timeSelectId,
                        '__token__':token
	                },
	                success(res) {
	                    console.log("//////////",res);
	                    if (res.code == 1) {
	                    	window.location.href=res.url;
	                    }else if (res.code == 1000) {
                            alert('您已提交过，请刷新页面重试！');
                        }
                        else{
	                    	alert(res.msg);
	                    }
	                },
	                error() {
	                    alert('确认预约请求失败!');
	                }
	            });
			});
			$(document).on('click', '.coach-item', function (e){
	            $('.coach-item-active').removeClass('coach-item-active');
	            $(this).addClass('coach-item-active');
	            $('.input-coach').children('div').text($(this).text());
	           $('.coach').slideUp(200);
	        });
	        $(document).on('click', '.input-coach', function (e){
	           e.stopPropagation();
	           $('.coach').slideDown(200);
	        });
	        $(document).on('click', function () {
	            $('.coach').slideUp(200);
	        });
    	});
    </script>
</body>

</html>